package cn.t.repository.impl.order;

import cn.t.converter.order.WorkOrderConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.order.WorkOrderDO;
import cn.t.persistence.entity.order.WorkOrderPO;
import cn.t.persistence.mapper.order.WorkOrderMapper;
import cn.t.repository.order.WorkOrderRepository;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工单表(WorkOrder)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:11
 */
@Slf4j
@Repository
@AllArgsConstructor
public class WorkOrderRepositoryImpl extends RepositoryImpl<WorkOrderMapper, WorkOrderPO, WorkOrderConverter, WorkOrderDO> implements WorkOrderRepository {


    @Override
    public List<WorkOrderDO> findByModel(WorkOrderDO workOrderDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<WorkOrderPO> list = getWrapper(workOrderDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<WorkOrderPO> getWrapper(WorkOrderDO workOrderDO) {
        LambdaQueryChainWrapper<WorkOrderPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(workOrderDO.getWorkOrderSn())) {
            wrapper.like(WorkOrderPO::getWorkOrderSn, workOrderDO.getWorkOrderSn());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getFirmId())) {
            wrapper.eq(WorkOrderPO::getFirmId, workOrderDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getDocType())) {
            wrapper.eq(WorkOrderPO::getDocType, workOrderDO.getDocType());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getAbnormalFlag())) {
            wrapper.eq(WorkOrderPO::getAbnormalFlag, workOrderDO.getAbnormalFlag());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getWorkOrderSource())) {
            wrapper.eq(WorkOrderPO::getWorkOrderSource, workOrderDO.getWorkOrderSource());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getMsgId())) {
            wrapper.eq(WorkOrderPO::getMsgId, workOrderDO.getMsgId());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getStatus())) {
            wrapper.eq(WorkOrderPO::getStatus, workOrderDO.getStatus());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getRemark())) {
            wrapper.eq(WorkOrderPO::getRemark, workOrderDO.getRemark());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getGmtStart())) {
            wrapper.eq(WorkOrderPO::getGmtStart, workOrderDO.getGmtStart());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getGmtEnd())) {
            wrapper.eq(WorkOrderPO::getGmtEnd, workOrderDO.getGmtEnd());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getProcessId())) {
            wrapper.eq(WorkOrderPO::getProcessId, workOrderDO.getProcessId());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getProcessId())) {
            wrapper.eq(WorkOrderPO::getProcessId, workOrderDO.getProcessId());
        }
        if (!ObjectUtils.isEmpty(workOrderDO.getNickname())) {
            wrapper.like(WorkOrderPO::getProcessId, workOrderDO.getNickname());
        }

        wrapper.ge(workOrderDO.getGmtCreatedStart() != null, WorkOrderPO::getGmtCreated, workOrderDO.getGmtCreatedStart());
        wrapper.lt(workOrderDO.getGmtCreatedEnd() != null, WorkOrderPO::getGmtCreated, workOrderDO.getGmtCreatedEnd());
        wrapper.in(!CollectionUtils.isEmpty(workOrderDO.getWorkOrderSns()), WorkOrderPO::getWorkOrderSn, workOrderDO.getWorkOrderSns());
        wrapper.in(!CollectionUtils.isEmpty(workOrderDO.getStatusList()), WorkOrderPO::getStatus, workOrderDO.getStatusList());
        wrapper.in(!CollectionUtils.isEmpty(workOrderDO.getEmpNos()), WorkOrderPO::getEmpNo, workOrderDO.getEmpNos());
        wrapper.ge(Boolean.FALSE.equals(workOrderDO.getExpired()), WorkOrderPO::getGmtExpire, new Date());
        wrapper.le(Boolean.TRUE.equals(workOrderDO.getExpired()), WorkOrderPO::getGmtExpire, new Date());
        wrapper.like(StringUtils.hasText(workOrderDO.getCreator()), WorkOrderPO::getCreator, workOrderDO.getCreator());
        return wrapper;
    }

    @Override
    public long count(WorkOrderDO workOrderDO) {
        return getWrapper(workOrderDO).count();
    }

    @Override
    public boolean updateWorkOrder(WorkOrderDO workOrderDO) {
        //只更新头表状态,暂不更新行表状态
        if (!StringUtils.hasText(workOrderDO.getWorkOrderSn())) {
            return false;
        }
        LambdaUpdateWrapper<WorkOrderPO> updateWrapper = new LambdaUpdateWrapper<WorkOrderPO>()
                .eq(WorkOrderPO::getWorkOrderSn, workOrderDO.getWorkOrderSn())
                .eq(workOrderDO.getFirmId() != null, WorkOrderPO::getFirmId, workOrderDO.getFirmId())
                .eq(StringUtils.hasText(workOrderDO.getProcessId()), WorkOrderPO::getProcessId, workOrderDO.getProcessId())
                .set(workOrderDO.getStatus() != null, WorkOrderPO::getStatus, workOrderDO.getStatus());

        return this.update(updateWrapper);
    }
}
